﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Vms.DB;
using Vms.Business;
using System.IO;

public partial class BongDa_LichThiDau : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Util m_util = new Util();
        DB m_db = new DB();
        WriteFile wf = new WriteFile();
        Util.TCharging m_tCharging = new Util.TCharging();
        try
        {
            int giaidauid = 0;
            int e_variable = 0;
            string d_variable = "";
            if (Request.Params["e"] != null)
            {
                e_variable = Int32.Parse(Request.Params["e"]);
            }
            if (Request.Params["desc"] != null)
            {
                d_variable = Request.Params["desc"];
            }
            if (Request.Params["giaidauid"] != null)
            {
                giaidauid = Int32.Parse(Request.Params["giaidauid"]);
            }
            //Update Session Table
            Util.TSession m_tSession = new Util.TSession();
            m_tSession.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
            m_tSession.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
            m_tSession.ServiceList = "13";
            m_tSession.KeyPress = "3";
            m_tSession.SessionID = "";
            m_tSession.IP = "";
            m_db.UpdateReQuestTbl(m_tSession);
            //Update Charging
            m_tCharging.ErrorCode = e_variable;
            m_tCharging.ErrorDescription = d_variable;
            m_tCharging.Service_ID = 13;
            m_tCharging.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
            m_tCharging.SessionID = Session.SessionID;
            m_tCharging.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
            m_tCharging.StartTime = DateTime.Now;
            string str = "<?xml version='1.0' encoding='ISO-8859-1'?>" +
                      "<!DOCTYPE pages SYSTEM 'cellflash.dtd'>" +
                      "<pages descr='Samis'>" +
                      "<page>";
            if (e_variable == 0)
            {
                m_tCharging.Cost = m_db.GetCostByService(m_tCharging.Service_ID);
                m_tCharging.CDRIndicator = "DATA";
                DataSet ds = new DataSet();
                ds = m_db.GetLTDByGiaiDau(giaidauid);
                DataTable dtb = new DataTable();
                dtb = ds.Tables[0].Clone();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                }
                string dates = "";
                foreach (DataRow dr in dtb.Rows)
                {
                    DateTime dt = DateTime.Parse(dr["start_time"].ToString().Trim());
                    if (dates != dt.ToString("dd/MM"))
                    {
                        dates = dt.ToString("dd/MM");
                        str += dates + "<br/>";
                    }
                    str += dt.ToString("hh:mm") + " " + dr["home"].ToString() + " - " + dr["away"].ToString() + "<br/>";
                }
            }
            else
            {
                m_tCharging.Cost = 0;
                m_tCharging.CDRIndicator = "WRONG";
                m_util.write_log(Request.Url.OriginalString, 2);
                m_util.WritelogHeader(Request.Headers);
                str += "Tai khoan khong du de su dung dich vu. Xin vui long nap tien!";
            }
            m_db.InsertChargingTbl(m_tCharging);



            str += "</page>" +
                   "</pages>";
            Response.ClearHeaders();
            Response.AddHeader("content-type", "text/xml");
            Response.Write(str);
            wf.WriteStreamFile(str);
            Response.End();
        }
        catch (Exception ex)
        {
            wf.WriteError(ex.ToString());
        }
    }
}